package rigo.ricard.pris.rs.ac.uns.dmi.util;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

public class CurrentUser {
	
	public static User getCurrentUser(FacesContext fc) {
		User u = null;
		ExternalContext externalContext = fc.getExternalContext();
		if (externalContext.getUserPrincipal() == null){
			System.out.println("current principal is null"); 
		}
		else{ 
			Long id = Long.parseLong(externalContext.getUserPrincipal().getName());
			try {
				u = UserLocalServiceUtil.getUserById(id); 
			}
			catch (PortalException ex) {
				ex.printStackTrace();
			}
			catch (SystemException ex) {
				ex.printStackTrace();
			} 
		} 
		return u;
	}
}
